"""

 * skype-plugin.py
 * This file is part of skype-plugin
 *
 * Copyright (C) 2007 - Jakub Krajniak
 *
 * skype-plugin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * any later version.
 *
 * skype-plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with skype-plugin; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 
"""

import rhythmdb, rb
import gobject, gtk
from Skype import Skype

class SkypePlugin(rb.Plugin):

	def __init__(self):
		rb.Plugin.__init__(self)
			
	def activate(self, shell):
		self.shell = shell
		player = shell.get_player()
		self.skype = Skype()
		self.psc_id = player.connect ('playing-song-changed', self.song_change)
		self.previous_status = self.skype.getStatus()
		self.lastStatus = ""
		if player.get_playing_entry():
			self.song_change (player, player.get_playing_entry())
		
	def deactivate(self, shell):
		self.shell.get_player().disconnect (self.psc_id)
		self.skype.setStatus(self.previous_status)
		del self.psc_id
		del self.db
		del self.shell
		del self.skype
		del self.out_connection
		del self.remote_bus
	def song_change(self, player, entry):
		artist = None
		title = None
		if entry:
			artist = self.get_song_info(entry)[0]
			title = self.get_song_info(entry)[1]
		response = ""
		if artist != None:
			response = artist
		if title != None:
			if response:
				response += " - " + title
			else:
				response = title
		newStatus = '['+response+']'
		if response and newStatus != self.lastStatus:
			self.skype.setStatus(newStatus)
			self.lastStatus = newStatus
		else:
			self.skype.setStatus(self.previous_status)
		
	def get_song_info(self, entry):
		self.db = self.shell.get_property('db')
		artist = self.db.entry_get (entry, rhythmdb.PROP_ARTIST) or None
		title = self.db.entry_get(entry,rhythmdb.PROP_TITLE) or None
		return (artist,title)

